package com.spzx.product.service.impl;

import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    BrandMapper brandMapper;

    @Override
    public List<Brand> selectBrandList(Brand brand) {
        String brandName = brand.getName();
        List<Brand> list = brandMapper.selectList(brandName);
        return list;
    }

    @Override
    public Brand findBrandInfo(Long id) {
        return brandMapper.findBrandInfo(id);
    }

    @Override
    public void update(Brand brand) {
        try {
            brandMapper.update(brand);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("品牌：" + brand.getName() + "，已经添加");
        }
    }

    @Override
    public void save(Brand brand) {
        try {
            brandMapper.save(brand);
        } catch (DuplicateKeyException e) {
            throw new ServiceException("品牌：" + brand.getName() + "，已经添加");
        }
    }

    @Override
    public void deleteBrand(Long[] ids) {
        brandMapper.deleteBrand(ids);
    }

    @Override
    public List<Brand> getBrandAll() {
        return brandMapper.selectList(null);
    }
}
